package com.web.servlet;


import com.util.DBUtil;
import com.util.TokenUtil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


@WebServlet(name = "LoginServlet", urlPatterns = "/LoginServ")
public class LoginServlet extends HttpServlet {

    
    public static boolean Check(String username,String password) throws SQLException {
        boolean status = false;
        // DBUtil dbUtil = new DBUtil();
        Connection connection= DBUtil.getConnection();
        Statement statement = null;
        ResultSet resultSet = null;

        statement = connection.createStatement();
        resultSet = statement.executeQuery("SELECT password from account where  username='"+username+"' and password='"+password+"'");
        if (resultSet.next()){
            return true;
        }
        DBUtil.close();
        return status;
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        Writer writer = response.getWriter();
        String s=request.getParameter("formData");

        // 接收json数据对象
        JSONObject json = JSONObject.fromObject(request.getParameter("formData"));
        // 获取json中的字段
        String username = (String) json.get("phone");
        String password = (String) json.get("pwd");
        String token = TokenUtil.makeToken();
        String name = null;

        // 连接数据库验证字段
        try {
            // 返回json对象
            JSONObject res = new JSONObject();
            JSONObject data = new JSONObject();
            // 如果账号密码验证成功
            if (Check(username,password)){
                // 获取name
                try {
                    Connection connection = DBUtil.getConnection();
                    Statement statement = null;
                    ResultSet resultSet = null;
                    statement = connection.createStatement();
                    resultSet = statement.executeQuery("SELECT name from account where  username='"+username+"' and password='"+password+"'");
                    if (resultSet.next()){
                        name = resultSet.getString("name");
                    }
                    DBUtil.close();
                } catch (SQLException troubles) {
                    troubles.printStackTrace();
                }
                
                res.put("code","1");// code字段，代表验证成功
                res.put("token",token);// token字段
                data.put("name",name);
                res.put("data",data);// data->json对象name字段
            }else {
                res.put("code","-1");// code字段
                res.put("msg","账号或密码错误，请重新输入！");
            }

            // 写response对象
            writer.write(res.toString());
            writer.flush();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
